package cn.ibizlab.odoo.core.odoo_mrp.service.impl;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.math.BigInteger;
import java.util.Collection;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import cn.ibizlab.odoo.core.client.model.Imrp_product_produce;
import cn.ibizlab.odoo.core.odoo_mrp.domain.Mrp_product_produce;
import cn.ibizlab.odoo.core.odoo_mrp.filter.Mrp_product_produceSearchContext;
import cn.ibizlab.odoo.core.odoo_mrp.service.IMrp_product_produceService;
import cn.ibizlab.odoo.util.SearchResult;
import cn.ibizlab.odoo.util.log.IBIZLog;

/**
 * 实体[记录生产] 服务对象接口实现
 */
@Slf4j
@IBIZLog
@Service
public class Mrp_product_produceServiceImpl implements IMrp_product_produceService {

    @Autowired
    private cn.ibizlab.odoo.core.client.service.Imrp_product_produceClientService mrp_product_produceClientService;

    @Override
    public boolean create(Mrp_product_produce mrp_product_produce) {
        boolean bOk = false ;
        Imrp_product_produce clientModel = this.convert2Model(mrp_product_produce,null);
        mrp_product_produceClientService.create(clientModel) ;
        this.convert2Domain(clientModel,mrp_product_produce);
        bOk = true ;

        return bOk ;
    }

    @Override
    public void updateBatch(List<Mrp_product_produce> mrp_product_produces, int batchSize) {

        //mrp_product_produceClientService.updateBatch(mrp_product_produces,batchSize) ;

    }

    @Override
    public void removeBatch(Collection<? extends Serializable> idList, int batchSize) {

        //mrp_product_produceClientService.removeBatch(mrp_product_produces) ;

    }

    @Override
    public void createBatch(List<Mrp_product_produce> mrp_product_produces, int batchSize) {

                        //mrp_product_produceClientService.createBatch(mrp_product_produces,batchSize) ;

    }

    @Override
    public boolean update(Mrp_product_produce mrp_product_produce) {
        boolean bOk = false ;
        Imrp_product_produce clientModel = this.convert2Model(mrp_product_produce,null);
        mrp_product_produceClientService.update(clientModel) ;
        this.convert2Domain(clientModel,mrp_product_produce) ;
        bOk = true;
        return bOk ;
    }

    @Override
    public boolean remove(Integer id) {
        boolean bOk = false ;
        Imrp_product_produce clientModel = mrp_product_produceClientService.createModel();
        clientModel.setId(id);
        mrp_product_produceClientService.remove(clientModel) ;
        bOk = true ;
        return bOk ;
    }

    @Override
    public Mrp_product_produce get(Integer id) {
        Imrp_product_produce clientModel = mrp_product_produceClientService.createModel();
        clientModel.setId(id);
        mrp_product_produceClientService.get(clientModel) ;
        return this.convert2Domain(clientModel,null);
    }

    @Override
    public Mrp_product_produce get(Mrp_product_produce mrp_product_produce) {
        Mrp_product_produce retmrp_product_produce = this.get(mrp_product_produce.getId()) ;
    	BeanCopier copier=BeanCopier.create(retmrp_product_produce.getClass(), mrp_product_produce.getClass(), false);
        copier.copy(retmrp_product_produce, mrp_product_produce, null);
        return mrp_product_produce ;
    }

    /**
     * 查询集合 默认查询
     */
    @Override
    public Page<Mrp_product_produce> searchDefault(Mrp_product_produceSearchContext context) {
        List<Mrp_product_produce> list = new ArrayList<Mrp_product_produce>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_product_produce> clientModelList = mrp_product_produceClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_product_produce clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_product_produce>(list, context.getPageable(), clientModelList.getTotalElements());
    }


    /**
     * 根据Production Order查询
     */
    @Override
    public Page<Mrp_product_produce> selectByOdoo_production(Integer id){
        Mrp_product_produceSearchContext context = new Mrp_product_produceSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mrp_product_produce> list = new ArrayList<Mrp_product_produce>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_product_produce> clientModelList = mrp_product_produceClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_product_produce clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_product_produce>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据产品查询
     */
    @Override
    public Page<Mrp_product_produce> selectByOdoo_product(Integer id){
        Mrp_product_produceSearchContext context = new Mrp_product_produceSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mrp_product_produce> list = new ArrayList<Mrp_product_produce>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_product_produce> clientModelList = mrp_product_produceClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_product_produce clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_product_produce>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mrp_product_produce> selectByOdoo_create(Integer id){
        Mrp_product_produceSearchContext context = new Mrp_product_produceSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mrp_product_produce> list = new ArrayList<Mrp_product_produce>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_product_produce> clientModelList = mrp_product_produceClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_product_produce clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_product_produce>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据用户查询
     */
    @Override
    public Page<Mrp_product_produce> selectByOdoo_write(Integer id){
        Mrp_product_produceSearchContext context = new Mrp_product_produceSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mrp_product_produce> list = new ArrayList<Mrp_product_produce>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_product_produce> clientModelList = mrp_product_produceClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_product_produce clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_product_produce>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据批次/序列号查询
     */
    @Override
    public Page<Mrp_product_produce> selectByOdoo_lot(Integer id){
        Mrp_product_produceSearchContext context = new Mrp_product_produceSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mrp_product_produce> list = new ArrayList<Mrp_product_produce>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_product_produce> clientModelList = mrp_product_produceClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_product_produce clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_product_produce>(list, context.getPageable(), clientModelList.getTotalElements());

    }

    /**
     * 根据产品计量单位查询
     */
    @Override
    public Page<Mrp_product_produce> selectByOdoo_product_uom(Integer id){
        Mrp_product_produceSearchContext context = new Mrp_product_produceSearchContext();
        if(!org.springframework.util.StringUtils.isEmpty(id)){
            cn.ibizlab.odoo.util.SearchFieldFilter fieldFilter=new cn.ibizlab.odoo.util.SearchFieldFilter();
            fieldFilter.setParam("id");
            fieldFilter.setCondition(cn.ibizlab.odoo.util.enums.SearchFieldType.EQ);
            fieldFilter.setValue(id);
            context.getCondition().add(fieldFilter);
        }
        List<Mrp_product_produce> list = new ArrayList<Mrp_product_produce>();
        Page<cn.ibizlab.odoo.core.client.model.Imrp_product_produce> clientModelList = mrp_product_produceClientService.search(context) ;
        for(cn.ibizlab.odoo.core.client.model.Imrp_product_produce clientModel : clientModelList){
            list.add( this.convert2Domain(clientModel,null));
        }
        return new PageImpl<Mrp_product_produce>(list, context.getPageable(), clientModelList.getTotalElements());

    }



    public Imrp_product_produce convert2Model(Mrp_product_produce domain , Imrp_product_produce model) {
        if(domain == null)
            return model ;
        if(model==null){
            model = mrp_product_produceClientService.createModel() ;
        }

         if(domain.getDisplay_nameDirtyFlag())
            model.setDisplay_name(domain.getDisplay_name());
         if(domain.get__last_updateDirtyFlag())
            model.set__last_update(domain.get__last_update());
         if(domain.getProduct_qtyDirtyFlag())
            model.setProduct_qty(domain.getProduct_qty());
         if(domain.getCreate_dateDirtyFlag())
            model.setCreate_date(domain.getCreate_date());
         if(domain.getSerialDirtyFlag())
            model.setSerial(domain.getSerial());
         if(domain.getProduce_line_idsDirtyFlag())
            model.setProduce_line_ids(domain.getProduce_line_ids());
         if(domain.getWrite_dateDirtyFlag())
            model.setWrite_date(domain.getWrite_date());
         if(domain.getIdDirtyFlag())
            model.setId(domain.getId());
         if(domain.getProduct_uom_id_textDirtyFlag())
            model.setProduct_uom_id_text(domain.getProduct_uom_id_text());
         if(domain.getCreate_uid_textDirtyFlag())
            model.setCreate_uid_text(domain.getCreate_uid_text());
         if(domain.getProduction_id_textDirtyFlag())
            model.setProduction_id_text(domain.getProduction_id_text());
         if(domain.getProduct_trackingDirtyFlag())
            model.setProduct_tracking(domain.getProduct_tracking());
         if(domain.getLot_id_textDirtyFlag())
            model.setLot_id_text(domain.getLot_id_text());
         if(domain.getProduct_id_textDirtyFlag())
            model.setProduct_id_text(domain.getProduct_id_text());
         if(domain.getWrite_uid_textDirtyFlag())
            model.setWrite_uid_text(domain.getWrite_uid_text());
         if(domain.getLot_idDirtyFlag())
            model.setLot_id(domain.getLot_id());
         if(domain.getCreate_uidDirtyFlag())
            model.setCreate_uid(domain.getCreate_uid());
         if(domain.getProduction_idDirtyFlag())
            model.setProduction_id(domain.getProduction_id());
         if(domain.getProduct_uom_idDirtyFlag())
            model.setProduct_uom_id(domain.getProduct_uom_id());
         if(domain.getWrite_uidDirtyFlag())
            model.setWrite_uid(domain.getWrite_uid());
         if(domain.getProduct_idDirtyFlag())
            model.setProduct_id(domain.getProduct_id());
        return model ;
    }

    public Mrp_product_produce convert2Domain( Imrp_product_produce model ,Mrp_product_produce domain) {
        if(model == null)
            return domain ;
        if(domain == null){
            domain = new Mrp_product_produce();
        }

        if(model.getDisplay_nameDirtyFlag())
            domain.setDisplay_name(model.getDisplay_name());
        if(model.get__last_updateDirtyFlag())
            domain.set__last_update(model.get__last_update());
        if(model.getProduct_qtyDirtyFlag())
            domain.setProduct_qty(model.getProduct_qty());
        if(model.getCreate_dateDirtyFlag())
            domain.setCreate_date(model.getCreate_date());
        if(model.getSerialDirtyFlag())
            domain.setSerial(model.getSerial());
        if(model.getProduce_line_idsDirtyFlag())
            domain.setProduce_line_ids(model.getProduce_line_ids());
        if(model.getWrite_dateDirtyFlag())
            domain.setWrite_date(model.getWrite_date());
        if(model.getIdDirtyFlag())
            domain.setId(model.getId());
        if(model.getProduct_uom_id_textDirtyFlag())
            domain.setProduct_uom_id_text(model.getProduct_uom_id_text());
        if(model.getCreate_uid_textDirtyFlag())
            domain.setCreate_uid_text(model.getCreate_uid_text());
        if(model.getProduction_id_textDirtyFlag())
            domain.setProduction_id_text(model.getProduction_id_text());
        if(model.getProduct_trackingDirtyFlag())
            domain.setProduct_tracking(model.getProduct_tracking());
        if(model.getLot_id_textDirtyFlag())
            domain.setLot_id_text(model.getLot_id_text());
        if(model.getProduct_id_textDirtyFlag())
            domain.setProduct_id_text(model.getProduct_id_text());
        if(model.getWrite_uid_textDirtyFlag())
            domain.setWrite_uid_text(model.getWrite_uid_text());
        if(model.getLot_idDirtyFlag())
            domain.setLot_id(model.getLot_id());
        if(model.getCreate_uidDirtyFlag())
            domain.setCreate_uid(model.getCreate_uid());
        if(model.getProduction_idDirtyFlag())
            domain.setProduction_id(model.getProduction_id());
        if(model.getProduct_uom_idDirtyFlag())
            domain.setProduct_uom_id(model.getProduct_uom_id());
        if(model.getWrite_uidDirtyFlag())
            domain.setWrite_uid(model.getWrite_uid());
        if(model.getProduct_idDirtyFlag())
            domain.setProduct_id(model.getProduct_id());
        return domain ;
    }


}
